<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Validate
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: Int.php 17470 2009-08-08 22:27:09Z thomas $
 */

/**
 * @see Zend_Validate_Abstract
 */
require_once 'Zend/Validate/Abstract.php';


/**
 * @category   Zend
 * @package    Zend_Validate
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Third_Validate_UserExists extends Zend_Validate_Abstract
{
    const INVALID = 'userExists';

    /**
     * @var array
     */
    protected $_messageTemplates = array(
        self::INVALID => "Ja existe um usuario com este login. Por favor escolha outro."
    );

    protected $login;
    protected $sites_id;
    protected $tipo;
    protected $dao;
    
    public function __construct( )
    {
    	$sites_id = Zend_Controller_Front::getInstance()->getRequest()->getParam('sites_id');
    	$tipo = Zend_Registry::get('userType');

        require_once("Usuarios.php");
        $dao = new Usuarios();

        $dao->setUserType( $tipo );
        $this->setSitesId( $sites_id );
        $this->setTipo( $tipo );
        $this->setDao($dao);
    }
	/**
	 * @param $tipo the $tipo to set
	 */
	public function setTipo($tipo) {
		$this->tipo = $tipo;
	}

	/**
	 * @return the $tipo
	 */
	public function getTipo() {
		return $this->tipo;
	}

	/**
	 * @param $dao the $dao to set
	 */
	public function setDao($dao) {
		$this->dao = $dao;
	}

	/**
	 * @return the $dao
	 */
	public function getDao() {
		return $this->dao;
	}

    
	/**
	 * @param $sites_id the $sites_id to set
	 */
	public function setSitesId($sites_id) {
		$this->sites_id = $sites_id;
	}

	/**
	 * @return the $sites_id
	 */
	public function getSitesId() {
		return $this->sites_id;
	}

	/**
	 * @param $login the $login to set
	 */
	public function setLogin($login) {
		$this->login = $login;
	}

	/**
	 * @return the $login
	 */
	public function getLogin() {
		return $this->login;
	}

    
    /**
     * Defined by Zend_Validate_Interface
     *
     * Returns true if and only if $value is a valid integer
     *
     * @param  string|integer $value
     * @return boolean
     */
    public function isValid( $value )
    {	
        if ( !is_string($value) ) {
            $this->_error(self::INVALID);
            return false;
        }
        
    	$valid = $this->getDao()->verifyLoginOnSite( $value, $this->getSitesId() );
    	if( count($valid) == 0 )
    		return true;

    	$this->_error(self::INVALID);
        return false;
    }
}
